﻿#if !UNITY_IOS || HUpdate
using System;
using System.Collections;
using System.Collections.Generic;
using System.Net;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;
using UnityEngine;

//下载控制
public static class DownloadClientConfig
{
    public static string ClientIp = "";

    public const int SpeedLimit_1M = 1048576;
    public static int SpeedLimitDefine = SpeedLimit_1M;

    public static int HighTaskMaxCount = 4;
    public static int LowTaskMaxCount = 2;
    //---------- 高速模式参数 ----------
    //高优先级下载任务同时存在数量限制
    public static int HighTaskMaxRunningCount = 4;

    //---------- 低速模式参数 ----------
    //低优先级下载任务同时存在数量限制
    public static int LowTaskMaxRunningCount = 2;

    //全速下载
    public static bool IsFullSpeed = true;
    
    //后台每线程下载限速
    public static int ThreadSpeedLimit = (int)(SpeedLimit_1M * 0.6);

    public static string Cookie = "";
    //断点续传标记
    public static bool HTTP_RESUME = false;

    public static UnityModule.DownloadTask.RequestMode DefaultDownloadType;
    //初始化设置
    public static void InitConfig()
    {
        //忽略100错误
        System.Net.ServicePointManager.Expect100Continue = false;
        System.Net.ServicePointManager.UseNagleAlgorithm = false;
        System.Net.ServicePointManager.CheckCertificateRevocationList = false;

        ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;
        //限制下载线程数
        System.Net.ServicePointManager.DefaultConnectionLimit =512;
    }

    public static bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors)
    {
        bool isOk = true;
        // If there are errors in the certificate chain,
        // look at each error to determine the cause.
        //if (errors != SslPolicyErrors.None)
        //{
        //    for (int i = 0; i < chain.ChainStatus.Length; i++)
        //    {
        //        if (chain.ChainStatus[i].Status == X509ChainStatusFlags.RevocationStatusUnknown)
        //        {
        //            continue;
        //        }
        //        chain.ChainPolicy.RevocationFlag = X509RevocationFlag.EntireChain;
        //        chain.ChainPolicy.RevocationMode = X509RevocationMode.Online;
        //        chain.ChainPolicy.UrlRetrievalTimeout = new TimeSpan(0, 1, 0);
        //        chain.ChainPolicy.VerificationFlags = X509VerificationFlags.AllFlags;
        //        bool chainIsValid = chain.Build((X509Certificate2)certificate);
        //        if (!chainIsValid)
        //        {
        //            isOk = false;
        //            break;
        //        }
        //    }
        //}
        return isOk;
    }

}
#endif